package UILayer;
import CtrLayer.OrderCtr;
import java.util.Scanner;
import java.util.ArrayList;
import ModelLayer.Stock;
import ModelLayer.OrderCollection;
import ModelLayer.CurrentAccount;
import ModelLayer.CustomerCollection;
/**
 * Write a description of class OrderUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class OrderUI
{
    private OrderCtr order_ctr;
    
    public OrderUI()
    {
        order_ctr = new OrderCtr();
    }
    
    public void start_order_menu()
    {
        boolean is_manager = !(CurrentAccount.give_rank().equals("employee"));
        boolean exit = false;
        while (!(exit))
        {
            int choice = write_order_menu(is_manager);
            if(is_manager)
            {
                switch(choice)
                {
                    case 1 :    add_order();
                                break;
                    case 2 :    complete_order();
                                break;
                    case 3 :    order_list();
                                break;
                    case 4 :    manage_discounts();
                                break;
                    default:    exit = true;
                                OrderCollection.save_data();
                                break;
                }
            }
            else
            {
                switch(choice)
                {
                    case 1 :    add_order();
                                break;
                    case 2 :    complete_order();
                                break;
                    case 3 :    order_list();
                                break;
                    default:    exit = true;
                                OrderCollection.save_data();
                                break;
                }
            }
        }
    }
        
    private void order_list()
    {
        boolean exit = false;
        
        while(!(exit))
        {
            int choice = write_order_list();
            switch(choice)
            {
                case 1 :    display_order();
                            break;
                case 2 :    display_pending_orders();
                            break;
                case 3 :    display_all_orders();
                            break;
                default:    exit = true;
                            break;
            }
        }
    }
    
    private void display_order()
    {
        String id;
        while((!(order_ctr.is_such_id(id = input_id()))) || id.equals("0"))
        {
            System.out.println("There is no such order with this id.");
        }
        
        if(!(id.equals("0")))
        {
            System.out.println(order_ctr.display_order(id));
        }
        wait_for_input();
    }
    
    private void display_pending_orders()
    {
        System.out.println(order_ctr.display_pending_orders());
        wait_for_input();
    }
    
    private void display_all_orders()
    {
        System.out.println(order_ctr.display_all_orders());
        wait_for_input();
    }
    
    private int write_order_menu(boolean is_manager)
    {
        System.out.println("\f***ORDER MENU***");
        System.out.println("Press (1) to add order.");
        System.out.println("Press (2) to close order");
        System.out.println("Press (3) to display orders");
        if(is_manager)
        {
            System.out.println("Press (4) to manage discounts.");
        }
        System.out.println("Press any other number to exit.");
        return int_input();
    }
     
    private int write_order_list()
     {
        System.out.println("\f***ORDER MENU***");
        System.out.println("Press (1) to display a particular order.");
        System.out.println("Press (2) to display pending orders.");
        System.out.println("Press (3) to display all orders.");
        System.out.println("Press any other number to exit.");
        return int_input();
     }
     
    private int int_input()
    {
        int input = 0;
        boolean ok = false;
        Scanner keyboard = new Scanner(System.in);
        while(!(ok))
        {
            try
            {
                input = keyboard.nextInt();
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("You must provide a valid input.\nYou have to type a number");
                keyboard.nextLine();
            }
        }
        return input;
    }
    
    private String string_input()
    {
        Scanner keyboard = new Scanner(System.in);
        return keyboard.nextLine();
    }
    
    private String input_id()
    {
        System.out.println("Please write the order id. Type (0) to return.");
        String id = "" + int_input();
        return id;
    }
    
     private boolean input_check()
    {
        String input = "";
        System.out.println("Type (y) for yes and (n) for no:");
        while(!((input = string_input()).equals("y") || input.equals("n")))
        {
            System.out.println("You provided incorrect input.");
        }
        if(input.equals("y"))
        {
            return true;
        }
        return false;
    }
    
    private String input_add_product()
    {
        System.out.println("Please write the number of the product. Type (0) to exit.");
        return string_input();
    }
    
    private String input_amount()
    {
        System.out.println("Please write the amount of the product. Type (0) to return");
        int temp_amount;
        while((temp_amount = int_input()) <= 0)
        {
            System.out.println("The amount must be greater than 0.");
        }
        String amount = "" + temp_amount;
        return amount;
        
    }
    
    private String input_customer_id()
    {
        System.out.println("Input the ID of a customer that placed the order. Press(0) to exit.");
        String customer_id = "" + int_input();
        return customer_id;
    }
    
    private boolean input_with_delivery()
    {
        String is_delivery;
        System.out.println("Enter (y) if the order is with delivery or (n) if it is not:");
        while(true)
        {
            is_delivery = string_input();
            if(is_delivery.equals("y"))
            {
                return true;
            }
            else if (is_delivery.equals("n"))
            {
                return false;
            }
            else
            {
                System.out.println("Incorrect, please write (y) if the order is with delivery or (n) if it is not:");
            }
        }
    }
    
    private String input_delivery_note()
    {
        System.out.println("Please write delivery note. Type (0) if you don't wish to add a note.");
        return string_input();
        
    }
    
    private String input_additional_information()
    {
        System.out.println("Please write additional information:");
        return string_input();
    }
    
    private String input_terms_of_delivery()
    { 
        System.out.println("Please write terms of delivery. Type (0) if you don't wish to input any terms.");
        return string_input();
    }
    
    private boolean input_finish_order()
    {
        String is_paid;
        System.out.println("Enter (y) if the order has been paid for or (n) if it hasn't");
        while(true)
        {
            is_paid = string_input();
            if(is_paid.equals("y"))
            {
                return true;
            }
            else if (is_paid.equals("n"))
            {
                return false;
            }
            else
            {
                System.out.println("Incorrect, please write (y) if the order has been paid for or (n) if it hasn't:");
            }
        }
    }
    
    private double input_discount()
    {
        boolean ok = true;
        double discount = 0;
        do
        {
            System.out.println("Insert maximum discount in percentages:");
            String del= string_input();
            if (del.endsWith("%"))
            {
                del = del.substring(0, del.length() - 1);
            }
            try
            {
                discount = Double.parseDouble(del);
                if ((discount < 0)||(discount > 100))
                {
                    System.out.println("The discount was not correct, please try again!");
                    ok = false;
                }
                else
                {
                    ok = true;
                }
            }
            catch (Exception e)
            {
                System.out.println("The discount was not correct, please try again!");
                ok = false;
            }
        }
        while (!ok);
        return discount;
    }  
           
    private void complete_order()
    {
        String id = "";
        while(!(order_ctr.is_such_id(id = input_id()) || id.equals("0")))
        {
            System.out.println("There is no order with this ID in the system.");
        }
        
        if(!(id.equals("0")))
        {
            if(order_ctr.is_completed(id))
            {
                System.out.println("The order has already been closed");
            }
            else
            {
               order_ctr.close_order(id);
               System.out.println("The order was successfully closed!");
            }
            wait_for_input();        
        }
    }
    
    private void add_order()
     {
         String customer_id;
         while(!(((customer_id = input_customer_id()).equals("0") || CustomerCollection.existing_customer(customer_id))))
         {
             System.out.println("Ther is no such customer.");
         }
         
         if(!(customer_id.equals("0")))
         {
             boolean with_delivery = input_with_delivery();
             String delivery_note = "0";
             String terms_of_delivery = "0";
             if (with_delivery)
             {
                 delivery_note = input_delivery_note(); 
                 terms_of_delivery = input_terms_of_delivery(); 
             }
             String additional_information = input_additional_information();
             ArrayList<String> ids = new ArrayList<>();
             ArrayList<String> amounts = new ArrayList<>();
             String[] id_amount = new String[2];
             
             while(!((id_amount = get_ids_amounts(ids))[0].equals("0")))
             {
                 ids.add(id_amount[0]);
                 amounts.add(id_amount[1]);
                 if(Stock.subtract_amount(id_amount[0], id_amount[1]))
                 {
                     System.out.println("Product's quantity has droped below minimum level.\nIt requires restocking.\n");
                     wait_for_input();
                 }
             }
             double special_discount = 0;
             if(!(CurrentAccount.give_rank().equals("employee")))
             {
                 System.out.println("Do you wish to make a special discount on the order?\nIf you will make it it will overwrite all other discounts for this order");
                 if(input_check())
                 {
                     special_discount = input_discount();
                }
             }
             
             if(!(ids.isEmpty()))
             {
                 boolean is_complete = input_finish_order();
                 double group_discount = CustomerCollection.get_group_discount(customer_id);
                 String id = order_ctr.add_order(with_delivery, delivery_note, additional_information, is_complete, terms_of_delivery, ids, amounts, special_discount, group_discount);
                 CurrentAccount.increase_total_sales(order_ctr.provide_total_cost_of_order(id));
                 CurrentAccount.add_new_serviced_order(id);
                 CustomerCollection.add_order_to_customer(customer_id, id);
                 System.out.println("The order was successfully created!");
             }
             else
             {
                 System.out.println("The order was not created, you have to add some products to it");
                 wait_for_input();
             }
         }
    }
    
    private String[] get_ids_amounts(ArrayList<String> ids)
    {
            String[] returner = new String[2];
            returner[0] = "0";
            returner[1] = "0";
            boolean wrong1 = true;
            boolean wrong2 = true;
            System.out.println("\fPress (1) to add new product to order.");
            System.out.println("Press (0) to exit.");
            String id;
            int choice;
            String amount;
            while(!((choice = int_input()) == 1 || choice == 0))
            {
                System.out.println("You provided incorrect input, press (1) to add new product or (0) to exit");
                wait_for_input();
            }
            
            if(choice == 1)
            {
                while(wrong1)
                {
                    if(Stock.does_such_product_exist(id = input_add_product()))
                    {
                        if(!(ids.contains(id)))
                        {
                            if(!(Stock.is_product_unfinished(id)))
                            {
                                returner[0] = id;
                                if((amount = input_amount()).equals("0"))
                                {
                                    returner[0] = "0";
                                    returner[1] = "0";
                                    wrong1 = true;
                                    wrong2 = false;
                                }                        
                                else if(Stock.is_such_amount(id, amount))
                                {
                                    returner[1] = amount;
                                    wrong1 = false;
                                    wrong2 = false;
                                    System.out.println("The product was successfully added!");
                                    wait_for_input();
                                }
                                else
                                {
                                    System.out.println("The quantity of the product is lower than the amount desired.");
                                }
                            }
                            else
                            {
                                System.out.println("The product has not been finished. \nUnfinished products cannot be added to orders.");
                            }
                        }
                        else
                        {
                            System.out.println("Product with this ID has already been added.");
                        }
                   }
                   else if(id.equals("0"))
                   {
                       returner[0] = "0";
                       returner[1] = "0";
                       break;
                   }
                   else
                   {
                       System.out.println("There is no product with this articule number.");
                   }
                }
            }
            return returner;
    }
    /**********************************************DISCOUNTS****************************************************/
    private void manage_discounts()
    {
        boolean wrong = true;
        int choice;
        while(wrong)
        {
            System.out.println("\fPress (1) to display current maximum discount.");
            System.out.println("Press (2) to change the maximum discount.");
            System.out.println("Press (3) to display current pickup discount.");
            System.out.println("Press (4) to change the pickup discount.");
            System.out.println("Press any other number to exit.");
            choice = int_input();
            switch(choice)
            {
                case 1 :    display_maximum_discount();
                            break;
                case 2 :    change_maximum_discount();
                            break;
                case 3 :    display_pickup_discount();
                            break;
                case 4 :    change_pickup_discount();
                            break;
                default:    wrong = false;
                            order_ctr.save_discounts();
                            break;
            }
        }
    }
    
    private void display_maximum_discount()
    {
        System.out.println(order_ctr.display_maximum_discount());
        wait_for_input();
    }
    
    private void change_maximum_discount()
    {
        order_ctr.change_maximum_discount(input_discount());
        System.out.println("The discount was successfully changed");
        wait_for_input();
    }
    
    private void display_pickup_discount()
    {
        System.out.println(order_ctr.display_pickup_discount());
        wait_for_input();
    }
    
    private void change_pickup_discount()
    {
        order_ctr.change_pickup_discount(input_discount());
        System.out.println("The discount was successfully changed");
        wait_for_input();
    }

    private void wait_for_input()
    {
        System.out.println("\nPRESS ENTER TO CONTINUE\n");
        Scanner keyboard = new Scanner(System.in);
        keyboard.nextLine();
    }
}
